<?php  
require_once('Item.php');

class SearchItems{

	public $department; //Department user would like to search in
	public $category;   //Category in above department user would like to search in
	public $manual;     //Manualy input parameters.... ("nike red")
	public $results;    //results of the database query

	public function __construct($department= '', $category = '', $manual = ''){
		$this->department = $department;
		$this->category = $category;
		$this->manual = $manual;
	}
	/**
	* Uses the class instance attributes to query the database, return an array
	*
	*/
	public function ResultsByCategory()
	{
		$id_array = array();
		$items_array = array();
		$con = mysqli_connect('localhost','mrc02_search','searchtest','mrc0260_sportinggoodscompany') or die(mysqli_error($con));
		
		//find the subcategories that correspond with the department
		$query = "SELECT id FROM categories WHERE parent_category_id = '$this->department'";
		$result = mysqli_query($con, $query) or die(mysqli_error($con));
		while($row = mysqli_fetch_array($result))
		{
			//store them in an array for below
			$id_Array[] = $row['id'];
		}
		
		//find the items that correspond to the subcategories found above
		$query = "SELECT id FROM items WHERE category_id IN ('".join("','", $id_Array)."')";
		$result = mysqli_query($con, $query) or die(mysqli_error($con));
		mysqli_close($con);
		while($id_array = mysqli_fetch_array($result))
		{
			$item = new Item;
			$item->retrieveInfo($id_array[0]);
			$item->displayItem();				
		}	
	}
	
	public function ResultsByType()
	{
		$id_array = array();
		$items_array = array();
		$con = mysqli_connect('localhost','mrc02_search','searchtest','mrc0260_sportinggoodscompany') or die(mysqli_error($con));
		
		//find the items that correspond to the subcategory
		$query = "SELECT id FROM items WHERE category_id = '$this->category'";
		$result = mysqli_query($con, $query) or die(mysqli_error($con));
		mysqli_close($con);
		while($id_array = mysqli_fetch_array($result))
		{
			$item = new Item;
			$item->retrieveInfo($id_array[0]);
			$item->displayItem();				
		}	
	}
}
?>